package org.xteam.sled.semantic;

import org.xteam.parser.runtime.Span;
import org.xteam.sled.semantic.exp.Exp;
import org.xteam.sled.semantic.exp.ExpForce;
import org.xteam.sled.semantic.exp.ExpNarrows;
import org.xteam.sled.semantic.exp.ExpNarrowu;


public class IntegerOperand extends OperandType {

	private boolean isSigned;
	private int width;

	public IntegerOperand(boolean isSigned, int width) {
		this.isSigned = isSigned;
		this.width = width;
	}

	@Override
	public Exp projectInt(IEvaluationContext context, Span span, Exp base) {
		if (width < 0)
			return base;
		if (isSigned)
			return new ExpNarrows(base, width);
		return new ExpNarrowu(base, width);
	}

	@Override
	public Exp makeExpression(Exp e, boolean relocatable) {
		e = relocatable ? new ExpForce(e) : e;
		if (width < 0 || ! isSigned) {
			return e;
		}
		return new ExpNarrows(e, width);
	}

}
